<?php

class Application_Model_BaseService
{
    protected $_dbTable;
    protected $dbTableName = '';
    protected $modelName = '';

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }
    
    public function getDbTable()
    {
        if (null === $this->_dbTable) {
        	$this->setDbTable($this->dbTableName);
        }
        return $this->_dbTable;
    }
    
    public function save($obj)
    {
        $data = array();
        $dbTable = $this->dbTableName;
        foreach($dbTable::getRowNames() as $name_row){
            $value = $obj->get($name_row);
            $data[$name_row] = $value;
        }
        
        if (isset($data['id'])) {
            $this->getDbTable()->update($data, array('id = ?' => $data['id']));
            return $data['id'];
        } else {
            return $this->getDbTable()->insert($data);
        }
    }
    
    public function mapToArray( $obj ){
    	$data = array();
        $dbTable = $this->dbTableName;
        foreach($dbTable::getRowNames() as $name_row){
            $value = $obj->get($name_row);
            if (isset($value)){
                $data[$name_row] = $value;
            }
        }
        return $data;
    }
    
    public function mapToObject( $data ){
        $obj = new $this->modelName();
        $dbTable = $this->dbTableName;
        foreach( $dbTable::getRowNames() as $name_row ){
            if (isset($data[$name_row])){
                $obj->set($name_row, $data[$name_row]);
            }
        }
        return $obj;
    }
    
    public function fetchAll()
    {
        $resultSet = $this->getDbTable()->fetchAll();
        foreach ($resultSet as $rows) {
            $obj = new $this->modelName();
            $objs[] = $this->mapToObject($rows);
        }
        return $objs;
    }
    
    public function findById($id){
    	$result = $this->getDbTable()->find($id);
    	if (count($result) == 1) {
            $rows = $result->current();
            $obj = $this->mapToObject($rows);
        }
        return $obj;
    }
    
    public function find($id){
    	return $this->getDbTable()->find($id);
    }
    
    public function delById($id)
    {
    	return $this->getDbTable()->delete(array('id = ?' => $id));
    }
    
    public function getPaginatorRows ($filter = null)
	{
        $newSelect = $this ->getDbTable()
                           ->select();
        if ($filter != null)
            $newSelect->where('name LIKE  ?', '%' . $filter . '%');
        
		$paginator = new Zend_Paginator(
            new Zend_Paginator_Adapter_DbSelect(
                $newSelect
            )
        );
        
		$paginator->setItemCountPerPage(5);
		$paginator->setPageRange(3);
		return $paginator;
	}
}

